package com.atxwq.shop.manager.serivce.impl;

import com.atxwq.shop.manager.mapper.ProductDetailsMapper;
import com.atxwq.shop.manager.mapper.ProductMapper;
import com.atxwq.shop.manager.mapper.ProductSkuMapper;
import com.atxwq.shop.manager.serivce.ProductService;
import com.atxwq.shop.model.dto.product.ProductDto;
import com.atxwq.shop.model.entity.product.Product;
import com.atxwq.shop.model.entity.product.ProductDetails;
import com.atxwq.shop.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 商品类
 * @Date 2025/5/15 08:24
 * @Created by 12336
 */
@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    /**
     * 查询所有商品
     *
     * @param page
     * @param limit
     * @param productDto
     * @return
     */
    @Override
    public PageInfo<Product> findByPage(Integer page, Integer limit, ProductDto productDto) {
        PageHelper.startPage(page, limit);
        List<Product> productList = productMapper.findByPage(productDto);
        return new PageInfo<>(productList);
    }

    /**
     * 保存商品规格（加载）
     * @param product
     */
    @Override
    public void save(Product product) {
        //1.保存商品基本信息，product
            product.setStatus(0);
            product.setAuditStatus(0);
           productMapper.save(product);
        //2.获取sku信息，保存sku信息到product_sku表
            //将SkuList集合中的每个sku设置productId
             int[] i = {1};
            product.getProductSkuList().forEach(productSku -> {
                //商品编号
                productSku.setSkuCode(product.getId()+"_"+ i[0]++);
                //商品id
                productSku.setProductId(product.getId());
                //skuname
                productSku.setSkuName(product.getName()+productSku.getSkuSpec());
                //销量
                productSku.setSaleNum(0);
                //状态
                productSku.setStatus(0);
                productSku.setIsDeleted(0);
                productSkuMapper.save(productSku);
            });


        //3.保存商品详情信息，product_details
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.save(productDetails);

    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Product getById(Long id) {
        //1.根据id查询商品
            Product product = productMapper.selectById(id);
        //2.根据id查询商品sku
       List<ProductSku> productSkus =  productSkuMapper.selectByProductId(id);
       product.setProductSkuList(productSkus);

       //3.根据id查询商品详细信息
        ProductDetails productDetails = productDetailsMapper.selectByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    /**
     * 修改商品（提交）
     * @param product
     */
    @Override
    public void updateById(Product product) {
        //1.修改基本商品数据
        productMapper.updateById(product);
        //2.修改sku数据
       List<ProductSku> productSkus = product.getProductSkuList();
       productSkus.forEach(productSku -> {
           productSkuMapper.update(productSku);
       });
        //3.修改商品详情数据
        // 修改商品的详情数据
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateById(productDetails);
    }

    /**
     * 删除商品
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //1.根据id删除商品基本数据
        productMapper.deleteById(id);
        //2,根据商品id删除商品的sku数据
        productSkuMapper.deleteByProductId(id);
        //3.根据商品id删除商品的详情数据
        productDetailsMapper.deleteByProductId(id);
    }

    /**
     * 审核服务方法
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
       Product product = new Product();
       product.setId(id);
       if(auditStatus == 1){
           product.setAuditStatus(1);
           product.setAuditMessage("审核通过");
       }
       else{
           product.setAuditStatus(-1);
           product.setAuditMessage("审核不通过");
       }
       productMapper.updateById(product);
    }

    /**
     * 商品上下架
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Product product = new Product();
        product.setId(id);
        if(status == 1){
            product.setStatus(1);
            log.info("商品上架" + product.getName());
        }
        else{
            product.setAuditStatus(-1);
            log.info("商品下架" + product.getName());
        }
        productMapper.updateById(product);
    }
}
